package com.just;

import com.just.relalm.CustomerRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

public class TestCustomerRealm {
    public static void main(String[] args) {
        // 1. 获取默认的securityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        // 2. 托管Realm
        securityManager.setRealm(new CustomerRealm());
        // 3. 设置工具类
        SecurityUtils.setSecurityManager(securityManager);
        // 4. 获取subject
        Subject subject = SecurityUtils.getSubject();
        // 5. 获取token
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123");
        // 6. 登录
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("认证失败， 用户名错误");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("认证失败， 密码错误");
        }


    }
}
